package com.example.androiduidemo.home;

import static android.content.Context.MODE_PRIVATE;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import java.util.List;

import com.example.androiduidemo.AddStudentActivity;
import com.example.androiduidemo.DatabaseOpenHelper;
import com.example.androiduidemo.approve.StudentAdapter;
import com.example.androiduidemo.bean.Student;
import cn.itcast.myapplication.databinding.FragmentHomeBinding;

public class HomeFragment extends Fragment {

    private FragmentHomeBinding binding;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        HomeViewModel homeViewModel =
                new ViewModelProvider(this).get(HomeViewModel.class);

        binding = FragmentHomeBinding.inflate(inflater, container, false);
        View root = binding.getRoot();


        return root;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        binding = null;
    }

    @Override
    public void onResume() {
        super.onResume();
        boolean isAdmin = getActivity().getSharedPreferences("data", MODE_PRIVATE).getBoolean("isAdmin", false);
        if (isAdmin) {
            binding.imageButton.setVisibility(View.VISIBLE);
        } else {
            binding.imageButton.setVisibility(View.GONE);
        }


        binding.imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AddStudentActivity.class);
                startActivity(intent);
            }
        });

        DatabaseOpenHelper dbHelper = new DatabaseOpenHelper(getActivity());
        List<Student> studentList = dbHelper.getAllStudents();
        binding.recyclerView.setAdapter(new StudentAdapter(studentList, getActivity()));
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));







    }
}